//引入 koa
const Koa = require("koa");
//引入koa-router
const Router = require("koa-router");
//引入数据
//实例化koa
const app = new Koa();
//实例化koa-router
const router = new Router();
//配置地址
router.get("/", (ctx, next) => {
	ctx.body = {
		code: 200,
		message: "我和小天天一起过了一个春节",
	};
});

// 商品详情的数据
let detailData = require("./datas/spuList.json");
router.get("/getdetailData", async (ctx, next) => {
	// 生成的H5应用存在跨域问题，通过CORS设置跨域配置
	ctx.set("Access-Control-Allow-Origin", "*");
	await new Promise((resolve) => setTimeout(() => resolve(), 2000));
	ctx.body = detailData;
});
let skuList = require("./datas/skuList.json");
router.get("/getskuList", async (ctx, next) => {
	// 生成的H5应用存在跨域问题，通过CORS设置跨域配置
	ctx.set("Access-Control-Allow-Origin", "*");
	await new Promise((resolve) => setTimeout(() => resolve(), 2000));
	ctx.body = skuList;
});
//获取购物车数据
let cartList = require("./datas/cartList.json");
router.get("/getcartList", async (ctx, next) => {
	ctx.set("Access-Control-Allow-Origin", "*");
	await new Promise((resolve) => setTimeout(() => resolve(), 2000));
	ctx.body = cartList;
});
router.get("/addcartList", async (ctx, next) => {
	ctx.set("Access-Control-Allow-Origin", "*");
	await new Promise((resolve) => setTimeout(() => resolve(), 2000));
	let map = new Map();
	let url = ctx.url
	let index = url.indexOf("?")//获取参数首字母下标
	let paramString = url.slice(index+1)//截取参数字符串
	let flag = paramString.indexOf("&")
	console.log(flag);
	if(flag==-1) { //判断是否是多个参数
		let p = paramString.split("=")
		map.set(p[0],p[1])
	} else{
		let paramArry = paramString.split("&");
		console.log(paramArry);
		for(let i = 0;i<paramArry.length;i++) {
			let item = paramArry[i];
			let p = item.split("=")
			map.set(p[0],p[1])
		}
	}
	console.log(map);
	//构建对象
	let cartItem = new Object();
	cartItem.id = map.get("id");
	cartItem.count = map.get("count");
	cartItem.imageUrl = map.get("imageUrl");
	cartItem.ischeakout = map.get("ischeakout");
	cartItem.price = map.get("price");
	cartItem.productName = map.get("productName");
	console.log(cartItem);
	let ind = cartList.length+1;
	cartList[ind] = cartItem;
	ctx.body = cartList;
});
// const initData = require('./datas/index.json')
// router.get('/getIndexData',(ctx,next)=>{

// 	ctx.body={
// 		code:200,
// 		initData
// 	}
// })
// 主页导航页面数据
// let indexCateList = require('./datas/indexCateList.json')
// router.get('/getindexCateList', async (ctx, next) => {
// 	// 生成的H5应用存在跨域问题，通过CORS设置跨域配置
// 	ctx.set('Access-Control-Allow-Origin', '*')
// 	await new Promise(resolve => setTimeout(() => resolve(),2000))
// 	ctx.body = indexCateList;
// });
//分类数据
let CategoryList = require("./datas/cartgory.json");
router.get("/categoryList", async (ctx, next) => {
	ctx.set("Access-Control-Allow-Origin", "*");
	await new Promise((resolve) => setTimeout(() => resolve(), 2000));
	ctx.body = CategoryList;
});
//使用路由及相关的方法
app.use(router.routes()).use(router.allowedMethods());
//监听端口
app.listen("3001", (err) => {
	if (err) {
		console.log("服务器启动失败,错误信息为", err.message);
		return;
	}
	console.log("服务器启动成功,地址为:http://localhost:3001");
});
